package me.yuxiaoyao.lock.redis;

import me.yuxiaoyao.lock.LockExecutor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.integration.redis.util.RedisLockRegistry;

/**
 * @author kerryzhang on 2020/12/22
 */


@Import(IntegrationRedisProperties.class)
public class RedisLockRegistryConfiguration {

    private final IntegrationRedisProperties integrationRedisProperties;

    public RedisLockRegistryConfiguration(IntegrationRedisProperties integrationRedisProperties) {
        this.integrationRedisProperties = integrationRedisProperties;
    }

    @Bean
    @ConditionalOnClass(RedisConnectionFactory.class)
    public RedisLockRegistry redisLockRegistry(RedisConnectionFactory factory) {
        return new RedisLockRegistry(factory, integrationRedisProperties.getRegistryKey());
    }

    @Bean
    @ConditionalOnMissingBean
    public LockExecutor lockExecutor(RedisLockRegistry redisLockRegistry) {
        return new RedisLockExecutor(redisLockRegistry);
    }


}
